﻿using System;
using System.Collections.Generic;
using System.Data;
using System.Data.Entity;
using System.Linq;
using System.Threading.Tasks;
using System.Net;
using System.Web;
using System.Web.Mvc;
using TGBTech.Core.Models.ChungTu;
using TGB.Core.Infrastructure;

namespace TGBTech.Web.Controllers
{
    public class CTChungTuController : Controller
    {
        private TGBTechDb db = new TGBTechDb();

        // GET: /CTChungTu/
        public async Task<ActionResult> Index()
        {
            var ctchungtu = db.CTChungTu.Include(c => c.ChiTietHangHoa).Include(c => c.ChungTu);
            return View(await ctchungtu.ToListAsync());
        }

        // GET: /CTChungTu/Details/5
        public async Task<ActionResult> Details(int? id)
        {
            if (id == null)
            {
                return new HttpStatusCodeResult(HttpStatusCode.BadRequest);
            }
            CTChungTu ctchungtu = await db.CTChungTu.FindAsync(id);
            if (ctchungtu == null)
            {
                return HttpNotFound();
            }
            return View(ctchungtu);
        }

        // GET: /CTChungTu/Create
        public ActionResult Create()
        {
            ViewBag.ChiTietHangHoaId = new SelectList(db.ChiTietHangHoa, "Id", "HangHoaId");
            ViewBag.ChungTuId = new SelectList(db.ChungTu, "Id", "Id");
            return View();
        }

        // POST: /CTChungTu/Create
        // To protect from overposting attacks, please enable the specific properties you want to bind to, for 
        // more details see http://go.microsoft.com/fwlink/?LinkId=317598.
        [HttpPost]
        [ValidateAntiForgeryToken]
        public async Task<ActionResult> Create([Bind(Include="Id,ChungTuId,NguoiGiaoDich,Kho,DiaDiem,ChiTietHangHoaId,SoLuong,DonGia,VAT")] CTChungTu ctchungtu)
        {
            if (ModelState.IsValid)
            {
                db.CTChungTu.Add(ctchungtu);
                await db.SaveChangesAsync();
                return RedirectToAction("Index");
            }

            ViewBag.ChiTietHangHoaId = new SelectList(db.ChiTietHangHoa, "Id", "HangHoaId", ctchungtu.ChiTietHangHoaId);
            ViewBag.ChungTuId = new SelectList(db.ChungTu, "Id", "Id", ctchungtu.ChungTuId);
            return View(ctchungtu);
        }

        // GET: /CTChungTu/Edit/5
        public async Task<ActionResult> Edit(int? id)
        {
            if (id == null)
            {
                return new HttpStatusCodeResult(HttpStatusCode.BadRequest);
            }
            CTChungTu ctchungtu = await db.CTChungTu.FindAsync(id);
            if (ctchungtu == null)
            {
                return HttpNotFound();
            }
            ViewBag.ChiTietHangHoaId = new SelectList(db.ChiTietHangHoa, "Id", "HangHoaId", ctchungtu.ChiTietHangHoaId);
            ViewBag.ChungTuId = new SelectList(db.ChungTu, "Id", "NgayCT", ctchungtu.ChungTuId);
            return View(ctchungtu);
        }

        // POST: /CTChungTu/Edit/5
        // To protect from overposting attacks, please enable the specific properties you want to bind to, for 
        // more details see http://go.microsoft.com/fwlink/?LinkId=317598.
        [HttpPost]
        [ValidateAntiForgeryToken]
        public async Task<ActionResult> Edit([Bind(Include="Id,ChungTuId,NguoiGiaoDich,Kho,DiaDiem,ChiTietHangHoaId,SoLuong,DonGia,VAT")] CTChungTu ctchungtu)
        {
            if (ModelState.IsValid)
            {
                db.Entry(ctchungtu).State = EntityState.Modified;
                await db.SaveChangesAsync();
                return RedirectToAction("Index");
            }
            ViewBag.ChiTietHangHoaId = new SelectList(db.ChiTietHangHoa, "Id", "HangHoaId", ctchungtu.ChiTietHangHoaId);
            ViewBag.ChungTuId = new SelectList(db.ChungTu, "Id", "NgayCT", ctchungtu.ChungTuId);
            return View(ctchungtu);
        }

        // GET: /CTChungTu/Delete/5
        public async Task<ActionResult> Delete(int? id)
        {
            if (id == null)
            {
                return new HttpStatusCodeResult(HttpStatusCode.BadRequest);
            }
            CTChungTu ctchungtu = await db.CTChungTu.FindAsync(id);
            if (ctchungtu == null)
            {
                return HttpNotFound();
            }
            return View(ctchungtu);
        }

        // POST: /CTChungTu/Delete/5
        [HttpPost, ActionName("Delete")]
        [ValidateAntiForgeryToken]
        public async Task<ActionResult> DeleteConfirmed(int id)
        {
            CTChungTu ctchungtu = await db.CTChungTu.FindAsync(id);
            db.CTChungTu.Remove(ctchungtu);
            await db.SaveChangesAsync();
            return RedirectToAction("Index");
        }

        protected override void Dispose(bool disposing)
        {
            if (disposing)
            {
                db.Dispose();
            }
            base.Dispose(disposing);
        }
    }
}
